<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/13
 * Time: 22:07
 */

namespace common\models\Service;


use common\models\Cache;
use common\models\CommonFunction;
use common\models\DAO\DayAccrual;
use common\models\DAO\ShareSetting;
use common\models\DAO\UserWalletLog;

class ShareService {
    public static function money($param){
        if(Wallet::remainder($param['userID']) < 1000)
            return false;
        $date = date('Ymd');
        $_today = strtotime($date);
        $share_count = DayAccrual::find()->where('userID=:userID and typeID=13 and createTime>=:today',
            ['userID'=>$param['userID'], 'today'=>$_today])->count('id');
        if($share_count >= 50){
            return false;
        }
        $dayAccrual = new DayAccrual();
        $dayAccrual->userID = $param['userID'];
        $dayAccrual->date = $date;
        $dayAccrual->money = $param['money'];
        $dayAccrual->invest = $param['money'];
        $dayAccrual->createTime = $param['createTime'];
        $dayAccrual->updateTime = $param['createTime'];
        $dayAccrual->typeID = $param['typeID'];
        $dayAccrual->chargeID = 0;
        $accrual_result  = $dayAccrual->save();
        if(!$accrual_result){
            return false;
        }
        //钱包明细
        $user_wallet = new UserWalletLog();
        $user_wallet->type = 1;
        $user_wallet->money = $param['money'];
        $user_wallet->createTime = $param['createTime'];
        $user_wallet->userID = $param['userID'];
        $user_wallet->road = $param['typeID'];
        $user_wallet->desc = $param['desc'];
        $user_wallet_result = $user_wallet->save();
        if(!$user_wallet_result){
            return false;
        }
        if(!empty($param['openID'])){
            SendWxMessage::charge($param['openID'], CommonFunction::formatMoney($param['money']), $param['desc'].'，您获得了'.CommonFunction::formatMoney($param['money']).'元佣金！');
        }
        return true;
    }
    public static function get($type, $userID = ''){
        $cacheName = 'share_'.$type;
        $cache = Cache::get($cacheName);
        if($cache)
            $result =  $cache;
        else{
            $share = ShareSetting::findOne($type);
            $result = [];
            if($share){
                $result['title'] = $share['title'];
                $result['desc'] = $share['desc'];
                $result['img'] = \Yii::$app->params['imgPrefix'].$share['img'];
                $result['href'] = $share['href'];
                Cache::set($cacheName, $result);
            }
        }
        if(!$result['href']){
            $url = \Yii::$app->request->getAbsoluteUrl();
            if($userID){
                $random = CommonFunction::getRandChar(6);
                $en_userID = $random.$userID;
                $url = \Yii::$app->params['host'] . '/join.html';
                if(strstr($url, '?')){
                    $url = $url.'&scyd_user='.$en_userID;
                }else{
                    $url = $url.'?scyd_user='.$en_userID;
                }
            }
            $result['href'] = $url;
        }
        $title = explode('%', $result['title']);
        $key = rand(0, count($title) - 1);
        $result['title'] = $title[$key];
        return $result;
    }
}